package com.jinhua.batch.config;

//import com.tencent.sr.rmall.springbootstarter.route.TsrExecutorsUtil;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//import org.springframework.tsf.core.util.NamedThreadFactory;
/**
 * Copyright
 *
 * @Author WuHuang
 * @Date 2023/3/31
 */
@Configuration
public class ThreadPoolConfig {
    private static final Logger log = LoggerFactory.getLogger(ThreadPoolConfig.class);

    public ThreadPoolConfig() {
    }

    @Bean(
            name = {"uploadTaskExecutor"}
    )
    public ExecutorService uploadTaskExecutor() {
        ThreadFactory threadFactory = new NamedThreadFactory("upload-task-thread");
        ThreadPoolExecutor executor = new ThreadPoolExecutor(20, 100, 1L, TimeUnit.MINUTES, new ArrayBlockingQueue(200), threadFactory);
        executor.allowCoreThreadTimeOut(true);
        executor.setRejectedExecutionHandler((r, executor1) -> {
            log.warn("thread pool is full");
        });
        return TsrExecutorsUtil.getTtlExecutor(executor);
    }

    @Bean(
            name = {"asyncCommonExecutor"}
    )
    public ExecutorService asyncCommonExecutor() {
        ThreadFactory threadFactory = new NamedThreadFactory("async-common-thread");
        ThreadPoolExecutor executor = new ThreadPoolExecutor(20, 100, 1L, TimeUnit.MINUTES, new ArrayBlockingQueue(200), threadFactory);
        executor.allowCoreThreadTimeOut(true);
        executor.setRejectedExecutionHandler((r, executor1) -> {
            log.warn("thread pool is full");
        });
        return TsrExecutorsUtil.getTtlExecutor(executor);
    }
}
